﻿using System;
using System.Collections.Generic;
using OpenTwitter.SDK;
using OpenTwitter.SDK.Tencent;
using OpenTwitter.SDK.Sina;
using OpenTwitter.SDK.Http;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace guanggun.guanggun
{
    public partial class Default : System.Web.UI.Page
    {
        private string appkey = ConfigurationManager.AppSettings["App_Key"];
        private string appsecret = ConfigurationManager.AppSettings["App_Secret"];
        private string sinaappkey = ConfigurationManager.AppSettings["sinaAppKey"];
        private string sinaappsecret = ConfigurationManager.AppSettings["sinaAppSecret"];
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Context.Session["appkey"] == null)
                {

                    Context.Session["accessKey"] = "";
                    Context.Session["accessSecret"] = "";
                    Context.Session["OauthVerify"] = "";
                    Context.Session["tokenKey"] = "";
                    Context.Session["tokenSecret"] = "";
                    Context.Session["appkey"] = appkey;
                    Context.Session["appsecret"] = appsecret;
                    Context.Session["comfirm"] = "0";//未认证
                    Session["sinaAppKey"] = sinaappkey;
                    Session["sinaAppSecret"] = sinaappsecret;
                }
            }
        }

       

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (Context.Session["appkey"] != null && Context.Session["appsecret"] != null)
            {
                OpenTwitter.SDK.Tencent.OAuth oauth = new OpenTwitter.SDK.Tencent.OAuth(Session["appkey"].ToString(), Session["appsecret"].ToString());
                if (string.IsNullOrEmpty(oauth.Token) || Context.Session["tokenKey"] == null)//如果teken不存在则获取
                {
                    string callback = "http://app.100mang.com/t.aspx";
                    if (oauth.GetRequestToken(callback))//获取成功
                    {
                        Context.Session["tokenKey"] = oauth.Token;
                        Session["tokenSecret"] = oauth.TokenSecret;
                        Response.Redirect("http://open.t.qq.com/cgi-bin/authorize?oauth_token=" + oauth.Token);
                    }
                    else
                    {
                        Response.Write("<script>alert('OMG,连接服务器失败，稍后再试。')</script>");
                    }
                }
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (Context.Session["sinaAppKey"] != null && Context.Session["sinaAppSecret"] != null)
            {
                OpenTwitter.SDK.Sina.OAuth sinaoauth = new OpenTwitter.SDK.Sina.OAuth(Session["sinaAppKey"].ToString(), Session["sinaAppSecret"].ToString());
                if (Session["sinaToken"] == null || string.IsNullOrEmpty(sinaoauth.Token))
                {
                    string callback = "http://app.100mang.com/s.aspx";
                   // string callback = "http://localhost:1146/s.aspx";
                    if (sinaoauth.GetRequestToken(callback))
                    {
                        Session["sinaToken"] = sinaoauth.Token;
                        Session["sinaTokenSecret"] = sinaoauth.TokenSecret;
                        Response.Redirect("http://api.t.sina.com.cn/oauth/authorize?oauth_token="+sinaoauth.Token);
                    }
                    else
                    {
                        Response.Write("<script>alert('OMG,连接服务器失败，稍后再试。')</script>");
                    }
                }
            }
            
            
        }
    }
}